<?php if(!defined('BASEPATH')) exit('No direct script access allowed!');

class Questions_Detail_Model extends grocery_CRUD_Model {
	protected $table_name = 'questions_detail';

	public function __construct() {
		parent::__construct();
	}
	
	public function get_latest_number($question_id){
		$this->db->select_max('original_number');
		$this->db->where('question_id',$question_id);
		$rs = $this->db->get($this->table_name);
		$rs = $rs->result_array();
		return $rs[0];
	}
	
	public function get_qty($question_id){
		$this->db->where('question_id',$question_id);
		$this->db->from($this->table_name);
		return $this->db->count_all_results();
	}
	
	function get_list()
    {
    	if($this->table_name === null)
    		return false;
    	
    	$select = "{$this->table_name}.*";

    	if(!empty($this->relation))
    		foreach($this->relation as $relation)
    		{
    			list($field_name , $related_table , $related_field_title) = $relation;
    			$unique_join_name = $this->_unique_join_name($field_name);
    			$unique_field_name = $this->_unique_field_name($field_name);
    			
				if(strstr($related_field_title,'{'))
    				$select .= ", CONCAT('".str_replace(array('{','}'),array("',COALESCE({$unique_join_name}.",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $unique_field_name";
    			else    			
    				$select .= ", $unique_join_name.$related_field_title as $unique_field_name";
    			
    			if($this->field_exists($related_field_title))
    				$select .= ", {$this->table_name}.$related_field_title as '{$this->table_name}.$related_field_title'";
    		}
    		
    	$this->db->select($select, false);
    	
		$this->db->join('questions','questions.id = question_id');
		$this->db->where('teacher_id',$_SESSION['person_id']);
		$this->db->where('question_id',$_SESSION['question_id']);
		
    	$results = $this->db->get($this->table_name)->result();
    	return $results;
    }
	
	function get_edit_values($primary_key_value)
    {
    	$primary_key_field = $this->get_primary_key();
    	$this->db->where($primary_key_field,$primary_key_value);
    	$result = $this->db->get($this->table_name)->row();
    	return $result;
    }

	function db_update($post_array, $primary_key_value)
    {
		$primary_key_field = $this->get_primary_key();
    	return $this->db->update($this->table_name,$post_array, array( $primary_key_field => $primary_key_value));
    }

	function db_insert($post_array)
    {
		$CI =& get_instance();
		$CI->load->model('questions_model');
		$qty = $CI->questions_model->get_qty($post_array['question_id']);
		$rs = $this->get_latest_number($post_array['question_id']);
		$latest_number = $rs['original_number'];
		$post_array['original_number'] = $latest_number+1;
		
		if($post_array['original_number'] <= $qty){
			$insert = $this->db->insert($this->table_name,$post_array);
			if($insert)
				return $this->db->insert_id();
		}
    	return false;
    }
	
	function db_delete($primary_key_value)
    {
    	$primary_key_field = $this->get_primary_key();
    	
    	if($primary_key_field === false)
    		return false;
    	
    	$this->db->limit(1);
    	$this->db->delete($this->table_name,array( $primary_key_field => $primary_key_value));
    	if( $this->db->affected_rows() != 1)
    		return false;
    	else
    		return true;
    }
	/*
	public function insert($data) {
		$data['username'] = $input->post('username');
		$data['nama'] = $input->post('nama');
		$data['password'] = md5($input->post('password'));
		$data['email'] = $input->post('email');
		$this->db->insert($this->_table_name, $data);
		return parent::last_inserted_id();
	}
	
	public function lists_array(){
		$rs = $this->db->get($this->_table_name)->result_array();
		$data = array();
		$data[0] = '';
		foreach($rs as $row){
			$data[ $row['id'] ] = $row['username'];
		}
		return $data;
	}
	
	public function get($id='') {
		$user = parent::get($id);
		return $user;
	}

	public function get_name($id) {
		$this->db->where('id',$id);
		$rs = $this->db->get($this->_table_name);
		$rs = $rs->result_array();
		return $rs[0]["nama"];
	}
	
	public function update(&$input, $id) {
		$user = $this->get($id);
		$data['username'] = $user['username'];
		$data['nama'] = $input->post('nama');
		$pass = $input->post('password');
		if($pass!='')
			$data['password'] = md5($pass);
		else
			$data['password'] = $user['password'];
		
		$data['email'] = $input->post('email');
		$this->db->update($this->_table_name, $data, array('id'=>$user['id']));
	}
	
	public function is_valid_to_delete($user_id){
		$ci = &get_instance();
		
		$ci->load->model('prospeklog_model','prospeklog');
		$prospeklog = $ci->prospeklog->get_by_user_id($user_id);
		if(count($prospeklog)>0)
			return false;
				
		return true;
	}
	
	public function delete($id){
		$this->db->delete($this->_table_name, array('id'=>$id));
	}

	public function change_password($user_id, $new_password) {
		$this->db->update($this->_table_name, array('password'=>$this->_encrypt($new_password)), array('id'=>$user_id));
	}

	public function encrypt_password($password) {
		return $this->_encrypt($password);
	}
	*/
}
